package com.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.service.AdminUserService;
import com.app.service.DepartmentService;
import com.app.model.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.DepartmentVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@Controller
@RequestMapping("/api/dep")
public class DepartmentController {
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private DepartmentService departmentService;


    @GetMapping("/getDepList")//部门管理分页查询
    public Result<Object> getDepList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String shortName,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        Page<DepartmentModel> depModelPage = departmentService.getPageDepList(page,limit,name,shortName);
        if(depModelPage == null){
            return Result.error(200,"暂无数据");
        }
        int count = (int)depModelPage.getTotal();
        List<DepartmentModel> depModelList;
        depModelList = depModelPage.getRecords();
        DepartmentVo vo = new DepartmentVo();
        vo.setCount(count);
        vo.setList(depModelList);
        return Result.success(vo);
    }

    @GetMapping("/getDepAllList")//查询全部
    public Result<Object> getDepAllList(
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        List<DepartmentModel> depModelList = departmentService.getAll();

        return Result.success(depModelList);
    }


    @PostMapping("/insertDep")//    新增/修改部门管理
    public Result<Object> insertDep(
            @RequestBody JSONObject json,
            HttpServletRequest request) {
        Integer id = Integer.valueOf(json.get("id").toString());
        String name = json.get("name").toString();
        String shortName = json.get("shortName").toString();
        Integer sort = Integer.valueOf(json.get("sort").toString());

        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        DepartmentModel model = new DepartmentModel();
        model.setName(name);
        model.setShortName(shortName);
        model.setStatus(1);
        model.setSort(sort);
            if(id > 0){
                model.setId(id);
                model.setUpTime(CommonHelp.getNowTimestamp());
                departmentService.update(model);
            }else {
                model.setCrtTime(CommonHelp.getNowTimestamp());
                departmentService.insert(model);
            }

        return Result.success(null);
    }
    @GetMapping("/updateDepStatus")//修改部门状态
    public Result<Object> updateStatus(
            @RequestParam("id") Integer id,
            @RequestParam("status") Integer status,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        DepartmentModel model = new DepartmentModel();
        model.setId(id);
        model.setStatus(status);
        departmentService.update(model);
        return Result.success(null);
    }
    @GetMapping("/selectDepById")//单查部门
    public Result<Object> selectDepById(
            @RequestParam("id") Integer id,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        DepartmentModel depModel = departmentService.getDepByDepId(id);
        if(depModel == null){
            return Result.error(500,"该部门不存在");
        }else {
            return Result.success(depModel);
        }
    }
    @GetMapping("/deleteDep")//删除基层单位
    public Result<Object> deleteDep(
            @RequestParam("id") Integer id,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")){
            return Result.error(405,"token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        departmentService.delete(id);
        return Result.success(null);
    }
}
